﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Data;
namespace LHWebApp.Json
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class JsonGetAlarmQueryInfoList : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string op = context.Request.Params["op"];
            switch (op)
            {
                case "strClientInfoList":
                    GetClientInfoList(context);
                    break;
                case "strVehicleInfoList":
                    GetVehicleListForAlarmQuery(context);
                    break;
                case "strAlarms":
                    GetAlarms(context);
                    break;
                case "strAlarmAllType":
                    GetAlarmAllType(context);
                    break;
                case "GetAlarmAllSelect":
                    GetAlarmAllSelect(context);
                    break;
                    
                default:
                    context.Response.Write("{success: false}");
                    break;
            }
        }
        private void GetAlarmAllType(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            try
            {
                DataTable dt = LHWebApp.ServicesVehicleAlarmInfo.GetAlarmAllType().Tables[0];
                JSONHelper jsonHelp = new JSONHelper();
                jsonHelp.success = true;
                jsonHelp.totlalCount = dt.Rows.Count;
                if (jsonHelp.totlalCount == 0)
                {
                    context.Response.Write("{success: true}");
                }
                else
                {
                    jsonHelp.AddItem("LICENSE_PLATE", "全部");
                    jsonHelp.AddItem("VID", "全部");
                    jsonHelp.ItemOk();
                    foreach (DataRow dr in dt.Rows)
                    {

                        jsonHelp.AddItem("LICENSE_PLATE", dr[0].ToString());
                        jsonHelp.AddItem("VID", dr[0].ToString());
                        jsonHelp.ItemOk();
                    }
                    context.Response.Write(jsonHelp.ToString());
                }
            }
            catch (Exception)
            {
                context.Response.Write("{success: false}");
            }
        }
        private void GetClientInfoList(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string clientNo = context.Request.Params["clientNo"];
            string currentService = context.Request.Params["currentService"];
            if (string.IsNullOrEmpty(clientNo))
            {
                context.Response.Write("{success: false}");
            }
            else
            {
                try
                {
                    DataTable dt = LHWebApp.Services.GetClientInfoList(currentService, clientNo).Tables[0];
                    JSONHelper jsonHelp = new JSONHelper();
                    jsonHelp.success = true;
                    jsonHelp.totlalCount = dt.Rows.Count;
                    if (jsonHelp.totlalCount == 0)
                    {
                        context.Response.Write("{success: true}");
                    }
                    else
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            jsonHelp.AddItem("CLIENT_NAME", dr["CLIENT_NAME"].ToString());
                            jsonHelp.AddItem("CLIENT_NO", dr["CLIENT_NO"].ToString());
                            jsonHelp.ItemOk();
                        }
                        context.Response.Write(jsonHelp.ToString());
                    }
                }
                catch (Exception)
                {
                    context.Response.Write("{success: false}");
                }
            }
        }
        private void GetVehicleListForAlarmQuery(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string clientNo = context.Request.Params["clientNo"];
            string currentService = context.Request.Params["currentService"];
  
            if (string.IsNullOrEmpty(clientNo))
            {
                context.Response.Write("{success: false}");
            }
            else
            {
                try
                {
                    DataTable dt = LHWebApp.Services.GetVehicleListForAlarmQuery(clientNo, currentService).Tables[0];
                    JSONHelper jsonHelp = new JSONHelper();
                    jsonHelp.success = true;
                    jsonHelp.totlalCount = dt.Rows.Count;
                    //context.Response.Write("{success:true,reason:'" + jsonHelp.totlalCount + "!',action:'1'}");
                    if (jsonHelp.totlalCount == 0)
                    {                        
                        context.Response.Write("{success: true}");
                    }
                    else
                    {
                        jsonHelp.AddItem("LICENSE_PLATE", "全部");
                        jsonHelp.AddItem("VID", "全部");
                        jsonHelp.ItemOk();
                        foreach (DataRow dr in dt.Rows)
                        {
                            jsonHelp.AddItem("LICENSE_PLATE", dr[0].ToString());
                            jsonHelp.AddItem("VID", dr[0].ToString());
                            jsonHelp.ItemOk();
                        }
                        context.Response.Write(jsonHelp.ToString());
                    }
                }
                catch (Exception)
                {
                    context.Response.Write("{success: false}");
                }
            }
        }
        private void GetAlarms(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string clientNo = context.Request.Params["clientNo"];
            if (string.IsNullOrEmpty(clientNo))
            {
                context.Response.Write("{success: false}");
            }
            else
            {
                try
                {
                    int limit = Int32.Parse(context.Request.Params["limit"]);
                    int start = Int32.Parse(context.Request.Params["start"]);
                    string sort = context.Request.Params["sort"];
                    string dir = context.Request.Params["dir"];
                    string StartTime = context.Request.Params["StartTime"];
                    string EndTime = context.Request.Params["EndTime"];
                    int AlarmType = Int32.Parse(context.Request.Params["AlarmType"]);
                    int Flag = Int32.Parse(context.Request.Params["Flag"]);
                    string LicensePlate = context.Request.Params["LicensePlate"];

                    DataTable dt = LHWebApp.ServicesVehicleAlarmInfo.GetAlarms(start, limit, sort, dir, StartTime, EndTime, AlarmType, Flag, LicensePlate).Tables[0];
                    JSONHelper jsonHelp = new JSONHelper();
                    jsonHelp.success = true;
                    jsonHelp.totlalCount = dt.Rows.Count;
                    if (jsonHelp.totlalCount == 0)
                    {
                        context.Response.Write("{success: true}");
                    }
                    else
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            jsonHelp.AddItem("License_Plate", dr["License_Plate"].ToString());
                            jsonHelp.AddItem("Alarm_Type", dr["Alarm_Type"].ToString());
                            jsonHelp.AddItem("LONGITUDE", dr["LONGITUDE"].ToString());
                            jsonHelp.AddItem("LATITUDE", dr["LATITUDE"].ToString());
                            jsonHelp.AddItem("Alarm_Time", dr["Alarm_Time"].ToString() == "" ? "" : DateTime.Parse(dr["Alarm_Time"].ToString()).ToString("yyyy-MM-dd hh:mm:ss"));
                            jsonHelp.AddItem("FLAG", dr["FLAG"].ToString());
                            jsonHelp.AddItem("OPERATOR", dr["OPERATOR"].ToString());
                            jsonHelp.AddItem("DISPOSE_TIME", dr["DISPOSE_TIME"].ToString() == "" ? "" : DateTime.Parse(dr["DISPOSE_TIME"].ToString()).ToString("yyyy-MM-dd hh:mm:ss"));
                            jsonHelp.ItemOk();
                        }
                        context.Response.Write(jsonHelp.ToString());
                    }
                }
                catch (Exception)
                {
                    context.Response.Write("{success: false}");
                }
            }
        }
        private void GetAlarmAllSelect(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string clientNo = context.Request.Params["clientNo"];
            string currentService = context.Request.Params["currentService"];
            string StartTime = context.Request.Params["StartTime"];
            string EndTime = context.Request.Params["EndTime"];
            string AlarmType = context.Request.Params["AlarmType"];
            string Flag = context.Request.Params["Flag"];
            string LicensePlate = context.Request.Params["LicensePlate"];
            try
            {
                DataTable dt = LHWebApp.ServicesVehicleAlarmInfo.GetAlarmAllSelect(clientNo, currentService, StartTime, EndTime, AlarmType, Flag, LicensePlate).Tables[0];//string clientNo, string type, string StartTime, string EndTime, int AlarmType, int Flag, string LicensePlate

                JSONHelper jsonHelp = new JSONHelper();
                jsonHelp.success = true;
                jsonHelp.totlalCount = dt.Rows.Count;
                if (jsonHelp.totlalCount == 0)
                {
                    context.Response.Write("{success: true}");
                }
                else
                {
                    foreach (DataRow dr in dt.Rows)
                    {

                        jsonHelp.AddItem("LICENSE_PLATE", dr["名称"].ToString());
                        //jsonHelp.AddItem("SIMCARD_NO", dr["SIMCARD_NO"].ToString());
                        jsonHelp.AddItem("ALARM_LOCATION", dr["报警位置"].ToString());
                        jsonHelp.AddItem("ALARM_TIME", dr["报警时间"].ToString() == "" ? "" : DateTime.Parse(dr["报警时间"].ToString()).ToString("yyyy-MM-dd hh:mm:ss"));
                        jsonHelp.AddItem("LONGITUDE", dr["经度"].ToString());
                        jsonHelp.AddItem("LATITUDE", dr["纬度"].ToString());
                        jsonHelp.AddItem("ALARM_TYPE", dr["报警类型"].ToString());
                        //jsonHelp.AddItem("ROWID", dr["ROWID"].ToString());
                        jsonHelp.ItemOk();
                    }
                    context.Response.Write(jsonHelp.ToString());
                }
            }
            catch (Exception)
            {
                context.Response.Write("{success: false}");
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
